# -*- coding:utf-8 -*-
import collections
from typing import List
import sys
import random
import heapq
from collections import defaultdict

class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None


class ListNode:
    def __init__(self, x):
        self.val = x
        self.next = None


def generateTreeNode(data):
    """
    根据bfs来复原二叉树
          1
    2         3
          4        5
    """
    if data == "[]":
        return
    vals, i = data[1:-1].split(','), 1
    root = TreeNode(int(vals[0]))
    queue = collections.deque()
    queue.append(root)
    while queue and i < len(vals):

        node = queue.popleft()
        if vals[i] != "null":
            node.left = TreeNode(int(vals[i]))
            queue.append(node.left)
        i += 1
        if vals[i] != "null":
            node.right = TreeNode(int(vals[i]))
            queue.append(node.right)
        i += 1
    print('树已生成完毕,', end='')
    PrintTree(root)
    return root


def generateListNode(nodeLists):
    n = len(nodeLists)
    head = ListNode(nodeLists[0])
    node = head
    for i in range(1, n):
        node.next = ListNode(nodeLists[i])
        node = node.next

    return head


def PrintTree(root):
    if root is None:
        sys.stdout.write("{}")
        return
    sys.stdout.write("该树为{")
    v = [root]
    c = 0
    none = ""
    while c < len(v):
        if c == 0:
            sys.stdout.write("%d" % v[c].val)
            v.append(v[c].left)
            v.append(v[c].right)
        else:
            if v[c] is None:
                none = none + ",null"
            else:
                sys.stdout.write("%s,%d" % (none, v[c].val))
                none = ""
                v.append(v[c].left)
                v.append(v[c].right)
        c = c + 1
    sys.stdout.write("}")
    print()
    print('------------------------------------------------')


def PrintListNode(head):
    if head is None:
        sys.stdout.write("{}")
        return
    current = head
    sys.stdout.write("{")
    while current:
        if current == head:
            sys.stdout.write("%d" % current.val)
        else:
            sys.stdout.write(",%d" % current.val)
        current = current.next

    sys.stdout.write("}")
    print()
    print('------------------------------------------------')


if __name__ == "__main__":
    a = generateListNode([1, 2, 3, 4])
    PrintListNode(a)
